package com.x.crm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.x.crm.model.Customer;
import com.x.crm.model.CustomerShop;
import com.x.crm.model.Intention;
import com.x.crm.model.IntentionModel;
import com.x.crm.rpc.CustomerRpcService;
import com.x.crm.rpc.CustomerShopRpcService;
import com.x.crm.rpc.IntentionRpcService;
import com.x.crm.util.Constant;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController

@RequestMapping("/customer")
public class CustomerController extends BaseController {


	@Reference
	private IntentionRpcService  intentionRpcService;

	@Reference
	private CustomerRpcService customerRpcService;

	@Reference
	private CustomerShopRpcService  customerShopRpcService;

	/**
	 * 新增客户及意向
	 * @param customer
	 * @param customerShop
	 * @param intention
	 * @param modelList
	 * @return
	 */
	@RequestMapping(value = "/insertSystemCustomer", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertSystemCustomer(Customer customer, CustomerShop customerShop, Intention intention, String modelList) {
		if (StringUtils.isEmpty(customerShop.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "店铺ID不能为空");
		}
		if (StringUtils.isEmpty(customer.getCustomerPhone())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "手机号不能为空");
		}
		if (StringUtils.isEmpty(customerShop.getCustomerShopId())) {
			customerShop.setCustomer(customer);
			List<CustomerShop> customerShopList = customerShopRpcService.selectSimpleCustomerShopList(customerShop);
			if (customerShopList != null && customerShopList.size() > 0){
				customerShop.setCustomerShopId(customerShopList.get(0).getCustomerShopId());
			}
		}
		customerShop.setFollowState(Constant.CUSTOMER_FOLLOWED);
		intention.setIntentionState(Constant.INTENTION_DGJ);
		intention.setIntentionType(Constant.INTENTION_BUY_TYPE); //购车意向
		intention.setIsOpportunity(Constant.IS_NOT_OPPORTUNITY); //个人录入
		customerShop.setCustomer(customer);
		intention.setCustomerShop(customerShop);
		intention.setCreateTime(this.getDate(0));
		intention.setUpdateTime(this.getDate(0));
		if (StringUtils.isNotEmpty(modelList)) {
			List<IntentionModel> models = JSON.parseArray(modelList, IntentionModel.class);
			intention.setIntentionModelList(models);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功", intentionRpcService.insertCustomerIntentionInfo(intention));
	}


	@RequestMapping(value = "/updateSystemCustomer", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateSystemCustomer(CustomerShop customerShop, Intention intention, String modelList) {
		if (StringUtils.isEmpty(customerShop.getCustomerShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客户ID不能为空");
		}
		if (StringUtils.isEmpty(intention.getIntentionId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "意向ID不能为空");
		}
		intention.setCustomerShop(customerShop);
		intention.setUpdateTime(this.getDate(0));
		if (StringUtils.isNotEmpty(modelList)) {
			List<IntentionModel> models = JSON.parseArray(modelList, IntentionModel.class);
			intention.setIntentionModelList(models);
		}
		intentionRpcService.updateCustomerIntentionInfo(intention);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "更新成功");
	}


	/**
	 * 查询手机号是否存在
	 * @param customer
	 * @param customerShop
	 * @return
	 */
	@RequestMapping(value = "/checkCustomerExist", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel checkCustomerExist(Customer customer, CustomerShop customerShop, Boolean isShopManage) {
		if (StringUtils.isEmpty(customer.getCustomerPhone())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "手机号不能为空");
		}
		if (StringUtils.isEmpty(customerShop.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车商ID不能为空");
		}
		if (StringUtils.isEmpty(customerShop.getFollowerId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "当前负责人ID不能为空");
		}
		if (isShopManage == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "当前登录是否是车商不能为空");
		}
		boolean canShow = true;
		boolean isPresent = false;
		customerShop.setCustomer(customer);
		String customerFollowerId = customerShop.getFollowerId();
		customerShop.setFollowerId("");
		Map<String, Object> res = new HashMap<>();
		List<CustomerShop> customerShopList = customerShopRpcService.selectSimpleCustomerShopList(customerShop);
		if (customerShopList != null  && customerShopList.size() > 0){
			CustomerShop customerInfo = customerShopList.get(0);
			if (customerInfo.getFollowerId() !=null && !customerInfo.getFollowerId().equals(customerFollowerId) && !isShopManage){
				canShow = false ;
			}
			isPresent = true;
			res.put("customerInfo", customerInfo);
		}else{
			res.put("customerInfo", new CustomerShop());
		}
		res.put("canShow", canShow);
		res.put("isPresent", isPresent);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}


	/**
	 * 客户分配
	 * @param customerShop
	 * @return
	 */
	@RequestMapping(value = "/allocatCustomerIntentionInfo", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel allocatCustomerIntentionInfo(CustomerShop customerShop) {
		if (StringUtils.isEmpty(customerShop.getFollowerId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "跟进人ID不能为空");
		}
		if (StringUtils.isEmpty(customerShop.getFollowerName())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "跟进人名称不能为空");
		}
		if (StringUtils.isEmpty(customerShop.getCustomerShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客户ID不能为空");
		}
		customerShop.setFollowState(Constant.CUSTOMER_FOLLOWED);
		customerShopRpcService.updateCustomerShopAlloctInfo(customerShop);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "分配成功");
	}


	/**
	 * 客户释放
	 * @param customerShop
	 * @return
	 */
	@RequestMapping(value = "/releaseCustomer", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel releaseCustomer(CustomerShop customerShop) {
		if (StringUtils.isEmpty(customerShop.getCustomerShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客户ID不能为空");
		}
		customerShopRpcService.updateCustomerShopReleaseInfo(customerShop);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "释放成功");
	}






	/**
	 * 查询客户详细信息
	 * @param customerShop
	 * @return
	 */
	@RequestMapping(value = "/resForApp/getCustomerContent", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel checkCustomerExist(CustomerShop customerShop ) {
		if (StringUtils.isEmpty(customerShop.getCustomerShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客户ID必传");
		}
		CustomerShop  customerShopInfo = new CustomerShop();
		List<CustomerShop> customerShopList = customerShopRpcService.selectSimpleCustomerShopList(customerShop);
		if (customerShopList != null && customerShopList.size() > 0 ){
			customerShopInfo = customerShopList.get(0);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", customerShopInfo);
	}




}
